layui.use(['layer', 'form', 'table', 'upload', 'dropdown', "laydate", "laydate",'tableSelect'], function () {
    var layer = layui.layer
        , form = layui.form
        , $ = layui.$
        , table = layui.table
        , upload = layui.upload
        , dropdown = layui.dropdown
        , laydate = layui.laydate
        ,tableSelect=layui.tableSelect


    laydate.render({
        elem: '.start' //指定元素
    });

    //新增证券下拉表单
    tableSelect.render({
        elem: 'input[name="accountName"]'
        , table: {
            url: './selectCashOne'
            , cellMinWidth: 60
            , cols: [
                [
                    {type: 'checkbox'},
                    {field: 'cashId', title: '现金账户ID'},
                    {field: 'accountName', title: '账户名称'},
                ]
            ]
        }
        , done: function (elem, data) {
            var count=data.data.length;
            if(count>1){
                layer.msg('不能选择多条现金账户!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
            }else if(count==0){
                layer.msg('请选择现金账户!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
            }else {
                $("input[name='accountName']").val(data.data[0].accountName);
                $("input[name='cashId']").val(data.data[0].cashId)
            }
        }
    })


    table.render({
        elem: '#FundsAllot'//表格的ID
        , url: '../selectCashAccrued'//表格的数据源 controller里的方法
        , cellMinWidth: 60 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,height:'full-180'
        , toolbar: '#userToolBar'//通过工具条的ID来区分
        , page: true//开启分页
        , limit: 5//默认进去每页显示5条
        , limits: [5, 10, 15]//自己设置下拉列表5,10,15，行
        , methods: "post"// 请求方式
        , cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            , {field: 'cashAccrued',  title: '现金应收应付id', align: 'center',hide: true}
            , {field: 'fundId', title: '基金Id', align: 'center', hide: true}
            , {field: 'businessTime',  title: '业务日期', align: 'center'}
            , {field: 'cashId', title: '现金账户Id', align: 'center', hide: true}
            , {field: 'accountName',  title: '现金账户名称', align: 'center'}
            , {
                field: 'businessType',  title: '业务类型', align: 'center',
                templet: function (item) {
                    if (item.businessType == 1) {
                        return '管理费';
                    } else if (item.businessType == 2) {
                        return '托管费';
                    } else if (item.businessType == 3) {
                        return '存款利息';
                    } else if (item.businessType == 4) {
                        return '申购赎回'
                    }else if (item.businessType == 6) {
                        return '托管费'
                    }else if (item.businessType == 7) {
                        return '管理费'
                    }
                }
            }
            , {
                field: 'fundFlow', title: '资金流向', align: 'center',
                templet: function (item) {
                    if (item.fundFlow == 1) {
                        return '流入';
                    }
                    return '流出';
                }
            }
            , {field: 'money',  title: '总金额', align: 'center'}
            , {field: 'remark',  title: '备注', align: 'center'}
            , {field: 'Text', title: '操作',  align: 'center', toolbar: '#toolEventDemo', fixed: 'right'}
        ]],
        //返回参数取名字
        request: {
            pageName: 'page',   // 页码的参数名称，默认：page
            limitName: 'row',  // 每页数据量的参数名，默认：limit
        }
    });

    //工具栏
    table.on('toolbar(FundsAllot)', function (obj) {
        var id = obj.config.id;
        var checkStatus = table.checkStatus(id);
        var data = checkStatus.data;
        switch (obj.event) {
            case 'search':
                Refresh()
                break;
            case 'deleteAll':
                if (data.length <= 0) {
                    layer.msg('请选择要从删除的行!', {icon: 0, time: 1000, shade: [0.5, '#000', true]});
                } else if (data.length == 1) {
                    layer.confirm('确认删除?', function () {
                        Delete(data, true)
                    })
                } else {
                    layer.confirm('确认批量删除?', function () {
                        Delete(data, true)
                    })
                }
                break;
            case 'add':
                $.post("./createId", {name:"CCP"}, function (msg) {
                    $("input[name='businessId']").val(msg)
                })
                var index = layer.open({
                    type: 1,
                    title: '新增现金应收应付',
                    closeBtn: 1,
                    move: false,
                    content: $("#addContent"),
                    btn: [],
                    area: ['700px', '500px'],
                    end: function () {
                        $("#addform")[0].reset()
                    },
                });
                break;
                form.render();
        }
    })

    //单元行
    table.on('tool(FundsAllot)', function (obj) { // 双击 toolDouble
        var data = obj.data;
        if (obj.event == 'del') {
            layer.confirm('确认删除?', function () {
                Delete(data.cashAccrued, false);
            });
        } else if (obj.event == 'edit') {
            form.val('editform', $.parseJSON(JSON.stringify(data)));
            var index = layer.open({
                type: 1,
                title: '修改现金应收应付',
                closeBtn: 1,
                move: false,
                area: ['700px', '500px'],
                content: $('#editContent')
            });
            form.render();
        }
    })

    function Delete(MyData, flag) {
        let Arr = [];
        if (!flag) {
            Arr.push(MyData)
        } else {
            for (var i = 0; i < MyData.length; i++) {
                Arr.push(MyData[i].cashAccrued);
            }
        }

        $.post("../deleteCashAccrued", {myArr: JSON.stringify(Arr)}, function () {
            if (Arr.length <= 1) {
                layer.msg('删除成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
            } else {
                layer.msg('批量删除成功!', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
            }
            Refresh()
        })
    }

    //表单刷新
    function Refresh() {
        var businessTime = $("#businessTime").val()
        var businessType = $("#businessType").val()
        table.reload('FundsAllot', {
            method: 'post'
            , where: {		//请求带的参数，json格式
                businessTime: $("#businessTime").val(),
                businessType: $("#businessType").val(),
            }
            , page: {		//分页参数
                curr: 1		//搜素后从第一页开始展示
            }
        });
        laydate.render({
            elem: '.start' //指定元素
        });
        $("#businessTime").val(businessTime)
        $("#businessType").val(businessType)
    }

    //新增提交
    form.on('submit(addsubmit)', function (data) {
        var formData = $('#addform').serialize();
        $.post("../addCashAccrued", formData, function (msg) {
            if (msg == "1") {
                table.reload('FundsAllot');
                layer.closeAll();
                layer.msg('添加成功', {icon: 1, time: 1000, shade: [0.5, '#000', true]})
                Refresh()
            }else{
                layer.msg('添加失败', {icon: 2, time: 1000, shade: [0.5, '#000', true]})
            }
        })
        return false;
    })

    //自定义验证规则
    form.verify({
        businessTime: function(value){
            if(value==null||value==""){
                return "业务日期不能为空"
            }
        },accountName:function (value){
            if(value==""||value==null){
                return "现金账户不能为空"
            }
        },businessType:function (value){
            if(value==""||value==null){
                return "业务类型不能为空"
            }
        },fundFlow:function (value){
            if(value<=0){
                return "资金流向不能为空"
            }
        },money:function (value){
            if(value<=0){
                return "总金额不能为空"
            }
        },Remark:function (value) {
            if(value==null||value==""){
                return "备注不能为空";
            }
        }
    });


    //修改提交
    form.on('submit(editsubmit)', function (data) {
        var formData = $('#editform').serialize();
        $.post("/updateCashAccrued", formData, function (msg) {
            if (msg == "ok") {
                table.reload('FundsAllot');
                layer.closeAll();
                layer.msg('修改成功', {icon: 1, time: 1000, shade: [0.5, '#000', true]});
                Refresh()
            } else if (msg == "error") {
                layer.msg('修改失败!', {icon: 2, time: 1000, shade: [0.5, '#000', true]});
            }
        });
        return false;
    })

    $(document).ready(function () {
        $(":button[name='offSubmit']").click(function () {
            $("#addform")[0].reset()
            layer.closeAll()
        })
    })
});
//取消框
function quxiao(){
    layer.closeAll();
}